package com.itheima.service;


import com.itheima.common.SnowFlake;
import com.itheima.mapper.admin.CategoryMapper;
import com.itheima.mapper.admin.DishMapper;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.pojo.admin.Category;
import com.itheima.pojo.admin.SetmealDish;
import com.itheima.pojo.client.selectDishListVo.SelectDishListVo;
import com.itheima.pojo.client.selectDishListVo.SelectFlavorListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 菜品列表service层
 */
@Service
public class DishService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //查询菜品列表
    public List<SelectDishListVo> selectDishList(String categoryId, Integer status) {

        //查询出菜品信息
        List<SelectDishListVo> selectDishListDtos = dishMapper.selectDishList(categoryId, status);

        if (selectDishListDtos.size()>0) {
            //获取分类名
            for (SelectDishListVo selectDishListDto : selectDishListDtos) {
                Category category = categoryMapper.categorySelectById(selectDishListDto.getCategoryId());
                if (category != null) {
                    selectDishListDto.setCategoryName(category.getName());
                }
            }


            //将菜品集合作为参数查询口味，返回出口味集合
            List<SelectFlavorListVo> selectFlavorListDtos = dishMapper.selectFlavorList(selectDishListDtos);

            //对菜品集合进行遍历
            for (SelectDishListVo selectDishListDto : selectDishListDtos) {

                //新建口味集合
                List<SelectFlavorListVo> list = new ArrayList<SelectFlavorListVo>();
                //将最后遍历后的口味集合放入菜品汇总
                selectDishListDto.setFlavors(list);

                //对口味集合进行遍历，与菜品id相对应的则添加到list集合中
                for (SelectFlavorListVo selectFlavorListDto : selectFlavorListDtos) {
                    if (selectDishListDto.getId().equals(selectFlavorListDto.getDishId())) {
                        list.add(selectFlavorListDto);
                    }
                }
            }
        }
        return selectDishListDtos;
    }
}
